<?php
namespace app\user\controller;
use \think\Controller;
use \think\Request;
use \think\Session;
use app\common\traits\controller\TraitController;
use app\common\controller\Api;
use app\user\traits\controllers\TraitUser;
use think\Lang;
/**
 * 前台房车分享
 */
class Order extends Api
{  
    use TraitUser;

    //前置操作
    protected $beforeActionList = [
        'checkLogin'
    ];
    /**
     * [checkLogin 判断是否登陆]
     * @return [type] [description]
     */
    protected function checkLogin()
    {
        if (!model('Users')->checkLogin()) {
            Session::set('redirect_url', Request::instance()->url(true));
            $this->redirect('@user/login');
            exit;
        }
    }

    public function getAllOrders()
    {
        $list = $this->getUserorders();
        $this->assign('list',$list);
        $count_list = count($list);
        $this->assign('count_list',$count_list);
        return view('order/allorders');
    }

    public function getOrderList()
    {
        return $this->getAllOrders();
    }

    public function getUnpayOrder()
    {
        $unpayedlist = $this->getUserorders(['stage' => '0']);
        $this->assign('unpayedlist',$unpayedlist);
        $count_unpayedlist = count($unpayedlist);
        $this->assign('count_unpayedlist',$count_unpayedlist);
        return view('order/unpayorders');
    }

    public function getPayedOrder()
    {
        $payedlist = $this->getUserorders(['stage' => '1']);
        $this->assign('payedlist',$payedlist);
        $count_payedlist = count($payedlist);
        $this->assign('count_payedlist',$count_payedlist);
        return view('order/payedorders');
    }

    public function getRefundOrder()
    {
        $refundlist = $this->getUserorders(['stage' => '2']);
        $this->assign('refundlist',$refundlist);
        $count_refundlist = count($refundlist);
        $this->assign('count_refundlist',$count_refundlist);
        return view('order/refundorders');
    }

    public function getCelOrder()
    {
        $cellist = $this->getUserorders(['stage' => '4']);
        $this->assign('cellist',$cellist);
        $count_cellist = count($cellist);
        $this->assign('count_cellist',$count_cellist);
        return view('order/celorders');
    }

    public function getDoneOrder()
    {
        $donelist = $this->getUserorders(['stage' => '3']);
        $this->assign('donelist',$donelist);
        $count_donelist = count($donelist);
        $this->assign('count_donelist',$count_donelist);
        return view('order/doneorders');
    }



    public function getOrderDetail($id)
    {

        $orderinfo = model('Orders')->getOrderInfo(['id' => $id]);//halt($orderinfo);
        $this->assign('orderinfo',$orderinfo);
        $user_id = session('user.id');
        if ($orderinfo->rent_contact_id) {
            //$rent_user_id = $orderinfo->rent_user_id;
            $contactinfo = model('ContactInfos')->where('id','in',$orderinfo->rent_contact_id)->select();
            $this->assign('contactinfo',$contactinfo);
        }else{
            $contactinfo = model('ContactInfos')->order('created_at desc')->where('user_id',$user_id)->find();
            $this->assign('contactinfo',$contactinfo);
        }
        
        return view('order/orderdetail');
    }

    public function getCancelOrder($id)
    {
        $this->cancelorder($id);
        return $this->success('取消订单成功','/order/allorders');
    }
	
    /**
     * 支付订单
     * @return [type] [description]
     */
    public function getPayOrder()
    {
        $param = model('Orders')->payOrder(['number' => $this->request->param('order_id')]);
        if (is_bool($param) && $param) {
            $this->success(Lang::get('Order has been paid successfully'), url("/order/allorders"));
        }else {
            $this->redirect(url('/pay/dopay', $param, true, 'api'));
        }
    }   
}

